using System;
using System.Collections.Generic;
using FireStarterModeller.UI;
using FireStarterModeller.UI.LookupLists;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Interfaces;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestFireStarterFormsController
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        private static IControlFactory GetControlFactory()
        {
            return GlobalUIRegistry.ControlFactory;
        }

        private static FireStarterFormsController CreateFireStarterFormsController()
        {
            return CreateFireStarterFormsController(GetControlFactory());
        }

        private static FireStarterFormsController CreateFireStarterFormsController(IControlFactory controlFactory)
        {
            return new FireStarterFormsController(controlFactory);
        }

        [Test]
        public void Test_Constructor()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GetControlFactory();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            FireStarterFormsController fireStarterFormsController = new FireStarterFormsController(controlFactory);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof (IControlFactory), fireStarterFormsController.ControlFactory);
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            IControlFactory factory = GetControlFactory();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            FireStarterFormsController fireStarterFormsController = CreateFireStarterFormsController();
            //---------------Test Result -----------------------
            Assert.AreSame(factory, fireStarterFormsController.ControlFactory);
        }

        [Test]
        public void Test_CreateNoneLookupTypeControl_ShouldCreateControl()
        {
            //---------------Set up test pack-------------------
            FireStarterFormsController fireStarterFormsController = CreateFireStarterFormsController();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ILookupListControl lookupListControl = fireStarterFormsController.CreateNoneLookupTypeControl();
            //---------------Test Result --------------------------
            Assert.IsNotNull(lookupListControl);
            Assert.IsInstanceOf(typeof (NoneLookupListControl), lookupListControl);
        }

        [Test]
        public void Test_CreateSimpleLookupTypeControl_ShouldCreateControl()
        {
            //---------------Set up test pack-------------------
            FireStarterFormsController fireStarterFormsController = CreateFireStarterFormsController();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ILookupListControl lookupListControl = fireStarterFormsController.CreateSimpleLookupTypeControl();
            //---------------Test Result --------------------------
            Assert.IsNotNull(lookupListControl);
            Assert.IsInstanceOf(typeof (SimpleLookupListControl), lookupListControl);
        }


        [Test]
        public void Test_CreateBusinessObjectLookupTypeControl_ShouldCreateControl()
        {
            //---------------Set up test pack-------------------
            FireStarterFormsController fireStarterFormsController = CreateFireStarterFormsController();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ILookupListControl lookupListControl = fireStarterFormsController.CreateBusinessObjectLookupTypeControl();
            //---------------Test Result --------------------------
            Assert.IsNotNull(lookupListControl);
            Assert.IsInstanceOf(typeof (BusinessObjectLookupListControl), lookupListControl);
        }

        [Test]
        public void Test_CreateShowErrorsForm_ShouldConstructForm()
        {
            //---------------Set up test pack-------------------
            List<IBOError> errors = new List<IBOError>();
            string title = TestUtilsShared.GetRandomString();
            string headingText = TestUtilsShared.GetRandomString();
            string descriptionText = TestUtilsShared.GetRandomString();
            ILabel headingLabel = new LabelWin();
            ILabel descriptionLabel = new LabelWin();
            IFormHabanero form = new FormWin();
            IPanel panel = new PanelWin();
            UserControlWin userControlWin = new UserControlWin();
            IControlFactory controlFactory = MockRepository.GenerateStub<IControlFactory>();
            controlFactory.Stub(t => t.CreateLabel(headingText, true)).Return(headingLabel);
            controlFactory.Stub(t => t.CreateLabel(descriptionText, true)).Return(descriptionLabel);
            IOKCancelDialogFactory okCancelDialogFactory = MockRepository.GenerateStub<IOKCancelDialogFactory>();
            okCancelDialogFactory.Stub(t => t.CreateOKCancelForm(userControlWin, title)).Return(form);
            controlFactory.Stub(t => t.CreateOKCancelDialogFactory()).Return(okCancelDialogFactory);
            controlFactory.Stub(t => t.CreateUserControl()).Return(userControlWin);
            controlFactory.Stub(t => t.CreatePanel()).Return(panel);
            controlFactory.Stub(t => t.CreateBorderLayoutManager(userControlWin)).Return(new BorderLayoutManagerWin(userControlWin, controlFactory));

            FireStarterFormsController fireStarterFormsController = CreateFireStarterFormsController(controlFactory);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IFormHabanero showErrorsForm = fireStarterFormsController.CreateShowErrorsForm(errors, title, headingText, descriptionText);
            //---------------Test Result -----------------------
            controlFactory.AssertWasCalled(t => t.CreateLabel(headingText, true));
            controlFactory.AssertWasCalled(t => t.CreateLabel(descriptionText, true));
            okCancelDialogFactory.AssertWasCalled(t => t.CreateOKCancelForm(userControlWin, title));
            Assert.AreSame(form, showErrorsForm);
            Assert.AreEqual(2, userControlWin.Controls.Count);
            Assert.AreSame(panel, userControlWin.Controls[1]);
            ErrorViewerControl errorViewerControl = TestUtilsShared.AssertIsInstanceOf<ErrorViewerControl>(userControlWin.Controls[0]);
            Assert.AreSame(errors, errorViewerControl.ErrorList);
            Assert.AreEqual(2, panel.Controls.Count);
            Assert.AreSame(headingLabel, panel.Controls[0]);
            Assert.AreSame(descriptionLabel, panel.Controls[1]);
        }
    }
}